const Application = require("koa");
const { jwt_config } = require("../config/config");
const jwt = require("jsonwebtoken")
const { getAllRoles, getUserbyUserNo, getUserInfobyCheckLogin, getAllUsersBySearch, addUser, deleteUserbyUserNo, updateUserbyUserNo } = require("../services/userService");

module.exports = {
    getAllRoles: async (ctx, next) => {
        try {
            let roles = await getAllRoles();
            if (roles) {
                ctx.body = {
                    success: true,
                    data: { roles: roles },
                    info: { msg: "获取角色列表成功" },
                    code: 200
                }
            } else {
                ctx.body = {
                    success: false,
                    data: { roles: roles },
                    info: { msg: "获取角色列表失败" },
                    code: 200
                }
            }
        } catch (error) {
            ctx.body = {
                success: false,
                data: {},
                info: { msg: "服务器请求异常" },
                code: 500
            }
        }
    },
    getUserInfoByUserno: async (ctx, next) => {
        try {
            // let Num =ctx.url;
            // let userNum =Num.substring(21)
            // console.log(userNum)
            // const userNum = ctx.session.userName
            const userNum = ctx.state.userNum
            console.log("ctx.state:");
            console.log(ctx.state);
            console.log(userNum);
            let users = await getUserbyUserNo(userNum);
            if (users) {
                ctx.body = {
                    success: true,
                    data: { users: users },
                    info: { msg: "获取用户成功" },
                    code: 200
                }
            } else {
                ctx.body = {
                    success: false,
                    data: { users: users },
                    info: { msg: "获取用户失败" },
                    code: 200
                }
            }
        } catch (error) {
            ctx.body = {
                success: false,
                data: {},
                info: { msg: "服务器请求异常" },
                code: 500
            }
        }
    },
    getUserLogin: async (ctx, next) => {
        try {
            console.log(ctx.request.body);
            const { userNum, password } = ctx.request.body;
            console.log(userNum + "-------" + password)

            let info = await getUserInfobyCheckLogin(userNum, password);
            if (info) {
                ctx.body = {
                    success: true,
                    data: { info: info },
                    info: { msg: "获取登录信息成功" },
                    code: 200
                }
            } else {
                ctx.body = {
                    success: false,
                    data: { info: info },
                    info: { msg: "获取登录信息失败" },
                    code: 200
                }
            }
        } catch (error) {
            ctx.body = {
                success: false,
                data: {},
                info: { msg: "服务器请求异常" },
                code: 500
            }
        }
    },
    getUserTokenByLogin: async (ctx, next) => {
        try {
            const { userNum, password } = ctx.request.body;
            console.log(userNum + "-------" + password)
            // const {userNum,password} = ctx.request.body;
            // console.log(password);
            let findUser = await getUserInfobyCheckLogin(userNum, password);
            if (findUser === null) {
                ctx.body = {
                    success: false,
                    data: {},
                    info: { msg: "账号密码错误" },
                    code: 401
                }
            } else {
                if (findUser) {
                    // console.log(findUser);
                    let payload = {
                        userNum: findUser.userNum
                    }
                    console.log(payload);
                    const token = jwt.sign(payload, jwt_config.secretKey, { expiresIn: jwt_config.tokenExpiresTime })
                    console.log(token);
                    ctx.body = {
                        success: true,
                        data: {
                            token: 'Bearer ' + token,
                            userInfo: findUser
                        },
                        info: { msg: "登录成功" },
                        code: 200
                    }
                }
            }
        } catch (error) {
            ctx.body = {
                success: false,
                data: {},
                info: { msg: "服务器请求异常" },
                code: 500
            }
        }
    },
    getSearchUserManage: async (ctx, next) => {
        try {
            console.log(ctx.request.body);
            const { userNum, username, userrole } = ctx.request.body;
            console.log(userNum, username, userrole);
            let seachResult = await getAllUsersBySearch(userNum, username, userrole);
            if (seachResult) {
                ctx.body = {
                    success: true,
                    data: { seachResult: seachResult },
                    info: { msg: "获取用户列表成功" },
                    code: 200
                }
            } else {
                ctx.body = {
                    success: false,
                    data: { seachResult: seachResult },
                    info: { msg: "获取用户列表失败" },
                    code: 200
                }
            }
        } catch (error) {
            ctx.body = {
                success: false,
                data: {},
                info: { msg: "服务器请求异常" },
                code: 500
            }
        }
    },
    addUser: async (ctx, next) => {
        try {
            console.log(ctx.request.body);
            const { userNum, username, sex, userrole } = ctx.request.body;
            console.log(userNum, username, sex, userrole);
            let users = await getUserbyUserNo(userNum);
            if (users) {
                ctx.body = {
                    success: false,
                    data: { users: users },
                    info: { msg: "该用户已存在" },
                    code: 200
                }
            } else {
                let addResult = await addUser(userNum, username, sex, userrole);
                if (addResult) {
                    ctx.body = {
                        success: true,
                        data: { addResult: addResult },
                        info: { msg: "添加用户成功" },
                        code: 200
                    }
                } else {
                    ctx.body = {
                        success: false,
                        data: { addResult: addResult },
                        info: { msg: "添加用户失败" },
                        code: 200
                    }
                }
            }
        } catch (error) {
            ctx.body = {
                success: false,
                data: {},
                info: { msg: "服务器请求异常" },
                code: 500
            }
        }

    },
    deleteUser: async (ctx, next) => {
        try {
            // console.log(ctx.request.body);
            // let userNum  = ctx.request.body;
            console.log(ctx.params.userNum);
            let userNum = ctx.params.userNum;
            console.log(userNum);
            if (userNum == 123456) {
                ctx.body = {
                    success: true,
                    data: {},
                    info: { msg: "管理员不可删除" },
                    code: 200
                }
            } else {
                let deleteResult = await deleteUserbyUserNo(userNum);
                if (deleteResult) {
                    ctx.body = {
                        success: true,
                        data: { deleteResult: deleteResult },
                        info: { msg: "删除用户成功" },
                        code: 200
                    }
                } else {
                    ctx.body = {
                        success: false,
                        data: { deleteResult: deleteResult },
                        info: { msg: "删除用户失败" },
                        code: 200
                    }
                }
            }
        } catch (error) {
            ctx.body = {
                success: false,
                data: {},
                info: { msg: "服务器请求异常" },
                code: 500
            }
        }
    },
    updateUser: async (ctx, next) => {
        try {
            console.log(ctx.request.body);
            const { userNum, userName, sex, roleId } = ctx.request.body;
            if (roleId == 1) {
                ctx.body = {
                    success: true,
                    data: {},
                    info: { msg: "管理员不可修改" },
                    code: 200
                }
            } else {
                let updateResult = await updateUserbyUserNo(userNum, userName, sex, roleId);
                console.log("updateResult", updateResult);
                if (updateResult) {
                    ctx.body = {
                        success: true,
                        data: { updateResult: updateResult },
                        info: { msg: "更新用户成功" },
                        code: 200
                    }
                } else {
                    ctx.body = {
                        success: false,
                        data: { updateResult: updateResult },
                        info: { msg: "更新用户失败" },
                        code: 200
                    }
                }
            }
        } catch (error) {
            ctx.body = {
                success: false,
                data: {},
                info: { msg: "服务器请求异常" },
                code: 500
            }
        }
    }
}